﻿using System;
using System.Web.Mvc;
using Kratos.Framework.Extensions;
using Kratos.Framework.Repositories.NHibernateProvider;

namespace Kratos.Framework.Filters
{
    //待定
    public class HibernateSessionAttribute : KratosFilterAttribute
    {
        public HibernateSessionAttribute() : base(typeof(HibernateSessionFilter)) { }
    }

    public class HibernateSessionFilter : IActionFilter
    {
        private readonly ISessionManager _sessionManager;
        public HibernateSessionFilter(ISessionManager sessionManager)
        {
            _sessionManager = sessionManager;
        }

        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
            _sessionManager.Unload();
        }

        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            try
            {
                var session = _sessionManager.Get();
            }
            catch (Exception ex)
            {
                Console.WriteLine("打开session错误:{0}".With(ex.Message));
            }
        }
    }
}
